"use strict";
cc._RF.push(module, '033f8jv5hdDTa3fc2Gzrt+V', 'game');
// Script/ui/game.js

"use strict";

exports.__esModule = true;
exports["default"] = void 0;

var _Common = _interopRequireDefault(require("../Common/Common"));

var _EnumData = _interopRequireDefault(require("../Common/EnumData"));

var _MyEventListener = _interopRequireDefault(require("../Common/myEventManger/MyEventListener"));

var _BaseUI = _interopRequireDefault(require("../core/BaseUI"));

var _DataManager = _interopRequireDefault(require("../core/DataManager"));

var _DataStorage = _interopRequireDefault(require("../core/DataStorage"));

var _ResourcesManager = _interopRequireDefault(require("../core/ResourcesManager"));

var _UIManager = _interopRequireDefault(require("../core/UIManager"));

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }

var game = cc.Class({
  "extends": _BaseUI["default"],
  properties: {
    Mname: {
      "default": null,
      type: cc.Label,
      tooltip: '歌曲名称'
    },
    putBox: {
      "default": null,
      type: cc.Node,
      tooltip: '放置盒子'
    },
    putItem: {
      "default": null,
      type: cc.Prefab,
      tooltip: '放置item预制'
    },
    putItemArr: {
      "default": [],
      type: cc.Node,
      tooltip: '放置图标数组'
    },
    targetBox: {
      "default": null,
      type: cc.Node,
      tooltip: '目标盒子'
    },
    targetItem: {
      "default": null,
      type: cc.Prefab,
      tooltip: '目标item预制'
    },
    targetArr: {
      "default": [],
      type: cc.Node,
      tooltip: '目标图标数组'
    },
    operation: {
      "default": null,
      type: cc.Node,
      tooltip: '操作界面'
    },
    ending: {
      "default": null,
      type: cc.Node,
      tooltip: '结束界面'
    },
    endIcon: {
      "default": null,
      type: cc.Sprite,
      tooltip: '拼接正确节点'
    },
    xingxing: {
      "default": null,
      tooltip: "星星-Node",
      type: cc.Node
    },
    animation: {
      "default": null,
      tooltip: "星星-Node",
      type: dragonBones.ArmatureDisplay
    }
  },
  // LIFE-CYCLE CALLBACKS:
  // onLoad () {},
  start: function start() {},

  /**初始游戏数据 */
  init: function init() {
    this.xingxing.active = false; // if (!this.animation)
    //     this.animation = this.xingxing.getComponent(dragonBones.ArmatureDisplay);
    // console.log(DataManager.GameConfig);

    if (!_DataManager["default"].currLevelConfig) _DataManager["default"].getCurrLevelConfig("DinosaurConfig", _DataManager["default"].currLevel); // console.log("开始游戏", DataManager.currLevel, DataManager.currLevelConfig);

    this.Mname.string = "LEVEL " + _DataManager["default"].currLevel; //DataManager.GameConfig["DinosaurLevel"][DataManager.currLevel].name;
    // httpUtils.httpPost(consts.HTTP_RECORD_SERVER, { title: '达到关卡', content: DataManager.currLevel, desc: null }, function () { });

    this.ending.active = false;
    this.operation.active = true;
    this.putItemArr = [];
    this.targetArr = [];
    this.initPutItems();
    this.initTargets();

    _MyEventListener["default"].on(_EnumData["default"].EventType.CheckPuzzle, this.onCheckPuzzle, this);
  },

  /**初始防止按钮 */
  initPutItems: function initPutItems() {
    // console.log("初始防止按钮111");
    var array = _DataManager["default"].currLevelConfig;

    for (var index = 0; index < array.length; index++) {
      var element = array[index];

      var item = _ResourcesManager["default"].getObjByPool("putItems", this.putItem);

      item.parent = this.putBox;
      var row = Math.floor(index / 4),
          col = index % 4;
      item.setPosition(-180 + 125 * col, 161 - row * 84);
      item.getComponent("Puzzle").init(element);
      this.putItemArr.push(item);
    } // console.log("初始防止按钮222")

  },

  /**初始目标数组 */
  initTargets: function initTargets() {
    // console.log("初始目标数组111");
    var array = _DataManager["default"].currLevelConfig;
    console.log(_DataManager["default"].currLevelConfig);

    for (var index = 0; index < array.length; index++) {
      var element = array[index];

      var item = _ResourcesManager["default"].getObjByPool("targetItems", this.targetItem);

      item.getComponent("target").init(element);
      item.parent = this.targetBox;
      this.targetArr.push(item);
    } // console.log("初始目标数组222");


    this.targetCount = 0;
    this.resultString = [];
    this.resultString.length = array.length;
  },

  /**重置结果界面 */
  resetEndings: function resetEndings() {
    this.endIcon.spriteFrame = null;
  },

  /**监听拼图放置，判定结果 */
  onCheckPuzzle: function onCheckPuzzle(param) {
    var minDic,
        minDicIndex = 0;

    for (var index = 0; index < this.targetArr.length; index++) {
      if (param.puzzleData.id == this.targetArr[index].targetData.id) {
        var worldPos = this.targetArr[index].parent.convertToWorldSpaceAR(this.targetArr[index].getPosition()),
            dic = _Common["default"].distance(worldPos, param.pos);

        !minDic ? (minDic = dic, minDicIndex = index) : dic < minDic ? (minDic = dic, minDicIndex = index) : null;
      }
    }

    var maxBorder = Math.min(this.targetArr[minDicIndex].width, this.targetArr[minDicIndex].height);
    maxBorder = Math.pow(maxBorder, 2);
    var result = null;

    if (minDic < maxBorder) {
      //放置成功
      result = this.targetArr[minDicIndex].getComponent(cc.Sprite).spriteFrame = _DataManager["default"].GameConfig[_DataManager["default"].currLevel][param.puzzleData.dino]; // this.targetArr[minDicIndex].putSuccess = true;

      this.targetArr[minDicIndex].data = param.puzzleData;
      this.resultString[minDicIndex] = param.puzzleData.name;
      this.targetCount++;
      appGame.audioMgr.playSFX("effect");
      this.xingxing.active = true;
      this.animation.playAnimation("xingxing", 1);
    }

    param.cb(result);

    if (this.targetCount >= this.targetArr.length) {
      this.setEnding();
    }
  },
  onClickFinish: function onClickFinish() {
    var _this = this;

    _DataManager["default"].showRewardedVideo(function () {
      for (var index = 0; index < _this.targetArr.length; index++) {
        var data = _DataManager["default"].currLevelConfig[index];
        _this.targetArr[index].getComponent(cc.Sprite).spriteFrame = _DataManager["default"].GameConfig[_DataManager["default"].currLevel][data.dino];
        _this.targetCount++;
      } // appGame.audioMgr.playSFX("effect");
      // this.xingxing.active = true;
      // this.animation.playAnimation("xingxing", 1);


      if (_this.targetCount >= _this.targetArr.length) {
        _this.setEnding();
      }
    }, function () {});
  },

  /**展示结果界面 */
  setEnding: function setEnding() {
    appGame.audioMgr.playSFX("pass");
    this.operation.active = false;
    this.clearGame();
    this.ending.active = true;
    this.endIcon.spriteFrame = _DataManager["default"].GameConfig[_DataManager["default"].currLevel][_DataManager["default"].GameConfig["DinosaurLevel"][_DataManager["default"].currLevel].type];
    this.unlockNextLevel();
    this.timeOutID1212 = setTimeout(function () {
      _DataManager["default"].showInterAd();
    }, 2000);
  },
  clearGame: function clearGame() {
    for (var index1 = 0; index1 < this.putItemArr.length; index1++) {
      _ResourcesManager["default"].putObjToPool("putItems", this.putItemArr[index1]);
    }

    this.putItemArr = []; // this.putBox.removeAllChildren();

    for (var index = 0; index < this.targetArr.length; index++) {
      _ResourcesManager["default"].putObjToPool("targetItems", this.targetArr[index]);
    }

    this.targetArr = []; // this.targetBox.removeAllChildren();
  },

  /**解锁下一关 */
  unlockNextLevel: function unlockNextLevel() {
    var _this2 = this;

    _DataManager["default"].currLevel += 1;
    _DataManager["default"].currLevel > _DataManager["default"].MaxLevel ? _DataStorage["default"].initStorage() : _DataStorage["default"].setLocalData("level", _DataManager["default"].currLevel);
    _DataManager["default"].currLevel = _DataStorage["default"].localData.level;

    _DataManager["default"].getCurrLevelConfig("DinosaurConfig", _DataManager["default"].currLevel);

    setTimeout(function () {
      _this2.timeOutID1212 && clearTimeout(_this2.timeOutID1212);

      _this2.enterNextLevel();
    }, 4000);
  },

  /**进入下一关 */
  enterNextLevel: function enterNextLevel() {
    _UIManager["default"].instance.load.open();

    if (_DataManager["default"].GameConfig[_DataManager["default"].currLevel]) {
      _UIManager["default"].instance.game.close();

      _UIManager["default"].instance.load.close();

      _UIManager["default"].instance.game.open();
    } else {
      _ResourcesManager["default"].loadAtlas("res/" + _DataManager["default"].GameConfig["DinosaurLevel"][_DataManager["default"].currLevel].type, function (data, pathName) {
        _UIManager["default"].instance.load.close();

        _DataManager["default"].GameConfig[_DataManager["default"].currLevel] = _Common["default"].arraytoJson(data, "name");

        _UIManager["default"].instance.game.close();

        _UIManager["default"].instance.game.open();
      });
    }
  },

  /**在玩一遍 */
  playAgain: function playAgain() {
    _UIManager["default"].instance.game.close();

    _UIManager["default"].instance.game.open();
  },
  open: function open() {
    this._super(arguments);

    _DataManager["default"].hideBannerAd();

    this.init();
  },
  close: function close() {
    this._super(arguments);

    this.clearGame();

    _MyEventListener["default"].off(_EnumData["default"].EventType.CheckPuzzle, this.onCheckPuzzle);
  },

  /**点击返回大厅 */
  onClickBack: function onClickBack(event) {
    this.close();

    _UIManager["default"].instance.hall.open();
  } // update (dt) {},

});
var _default = game;
exports["default"] = _default;
module.exports = exports["default"];

cc._RF.pop();